﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebAppPlanProduccion.Code;
using Web.Framework;
using WebAppPlanProduccion.Code.Security;
using PlanProd.Contracts;

namespace WebAppPlanProduccion.App
{
    public partial class PedidoComercialList : BasePage
    {        
        protected void Page_Load(object sender, EventArgs e)
        {
            HasPermissionToSeeMe(PlanProdSecurity.PEDIDOCOMERCIAL_VER);

            if (!this.IsPostBack)
            {
                UIHelper.LoadTemporadas(this.cboTemporada);
                UIHelper.LoadDestinos(this.cboDestino, "Todos");
            }
        }

        protected override void ApplySecurity()
        {
            this.lnkCrearPedido.Visible = HasPermissionFor(PlanProdSecurity.PEDIDOCOMERCIAL_CREAR);

            if (this.GridPedidosComerciales.DataSource != null)
            {
                this.GridPedidosComerciales.Columns[0].Visible = HasPermissionFor(PlanProdSecurity.PEDIDOCOMERCIAL_EDITAR);
                this.GridPedidosComerciales.Columns[1].Visible = HasPermissionFor(PlanProdSecurity.PEDIDOCOMERCIAL_ELIMINAR);
                this.GridPedidosComerciales.Columns[2].Visible = HasPermissionFor(PlanProdSecurity.PEDIDOCOMERCIAL_DETALLE_IMPORTAR);
            }
        }

        protected void lnkBuscar_Click(object sender, EventArgs e)
        {
            try
            {
                Buscar();
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        private void Buscar()
        {
            // cargo los filtros
            int? pedidoId = UIHelper.GetIntFromInputText(this.txtPedidoId.Text);
            int? temporadaId = UIHelper.GetIntFromInputCbo(this.cboTemporada);
            int? destinoId = UIHelper.GetIntFromInputCbo(this.cboDestino);
            DateTime? fechaDesde = UIHelper.GetDateTimeFromInputText(this.txtFechaDesde.Text);
            DateTime? fechaHasta = UIHelper.GetDateTimeFromInputText(this.txtFechaHasta.Text);
            string nombreLike = this.txtNombre.Text;
            
            this.GridPedidosComerciales.DataSource = WebServiceHelper.GetPedidosComerciales(pedidoId, temporadaId, destinoId, fechaDesde, fechaHasta, nombreLike);
            this.GridPedidosComerciales.DataBind();
        }

        protected void GridPedidosComerciales_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                this.GridPedidosComerciales.PageIndex = e.NewPageIndex;

                Buscar();
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void lnkCrearPedido_Click(object sender, EventArgs e)
        {
            this.Response.Redirect("/App/PedidoComercialCreate.aspx", true);
        }

        protected void GridPedidosComerciales_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                if (e.CommandName == "editar")
                {
                    int pedidoComercialId = Convert.ToInt32(this.GridPedidosComerciales.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);
                    this.Response.Redirect(string.Format("/App/PedidoComercialEdit.aspx?Id={0}", pedidoComercialId), true);
                }
                else if (e.CommandName == "eliminar")
                {
                    int pedidoComercialId = Convert.ToInt32(this.GridPedidosComerciales.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);
                    WebServiceHelper.EliminarPedidoComercial(pedidoComercialId);
                    Buscar();                    
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void GridPedidosComerciales_RowDataBound(object sender, GridViewRowEventArgs e)
        {           
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Attributes.Add("title", "Editar");
                e.Row.Cells[1].Attributes.Add("title", "Eliminar");

                PedidoComercial pedidoComercial = (PedidoComercial)e.Row.DataItem;                
                
                ImageButton btnEliminarCtrl = (ImageButton)e.Row.FindControl("btnEliminar");
                btnEliminarCtrl.OnClientClick = string.Format("return confirm('Está seguro que desea eliminar el siguiente registro? {0}');", pedidoComercial.Id);
            }        
        }
    }
}